<?php
/**
 * Created by lzl
 * Date: 2021 2021/10/11
 * Time: 13:24
 */
namespace App\Services\Api;
use App\Forms\AddressForms;
use App\Forms\GoodsAttributeForms;
use App\Forms\GoodsForms;
use App\Forms\OrderForms;
use App\Forms\UserBehaviorForms;
use App\Forms\UserForms;
use App\Models\Good;
use App\Models\User;
use App\Models\UserGoodsBehavior;
use App\Models\UserShareCode;
use App\Services\BaseServices as Base;


class UserServices extends Base
{


    public function center()
    {
        $data['userInfo']  = $this->userDetail()->toArray();
        $data['userInfo']['partner_end'] = !$data['userInfo']['partner_end'] ?: date('Y-m-d', strtotime($data['userInfo']['partner_end']));
        $data['orderCount'] = (new OrderForms())->getUserOrderCount(UID);

        $data['balance'] = (new UserForms())->getUserBalanceCount();
        //自营数据
        $data['childrenCount'] = (new UserForms())->getUserOrderCount();
        return $data;
    }

    /**
     * 用户详情
     * @return \App\Models\User|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function userDetail()
    {
        return (new UserForms())->getUserDetail(UID, [
           'id', 'nick_name', 'avatar', 'mobile', 'sex', 'birthday', 'level', 'balance', 'level', 'pid', 'partner_end'
        ]);
    }


    /**
     * 修改用户资料
     * @param $validated
     * @return User|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function editUserMsg($validated)
    {
        $userForms = new UserForms();

        $user =  $userForms->getUserDetail(UID);

        //标记上级用户
        if(isset($validated['p_openid']) && $validated['p_openid'] && empty($user->pid)){
            $validated['pid'] = User::where('openid', $validated['p_openid'])->first()->id ?? 0;
            unset($validated['p_openid']);
        }

        if(isset($validated['source']) ){
            if(!empty($user->source)){
                unset($validated['source']);
            }
        }

        $user->update($validated);

        return  $user->refresh();
    }


    /**
     * 添加用户行为
     * @param $validated
     * @return UserGoodsBehavior|\Illuminate\Database\Eloquent\Model
     */
    public function addUserBehavior($validated)
    {
        return (new UserBehaviorForms())->setBehavior($validated['goods_id'], UID, $validated['type']);
    }

    /**
     * 收货地址列表
     * @param $validated
     * @return mixed
     */
    public function addressList($validated)
    {
        return (new AddressForms())->addressList(array_merge($validated, ['uid' => UID]));
    }


    /**
     * 修改收货地址
     * @param $validated
     * @return \App\Models\UserAddress|\Illuminate\Database\Eloquent\Model
     */
    public function editAddress($validated)
    {
        return (new AddressForms())->editAddress(array_merge($validated, ['uid' => UID]));
    }

    /**
     * 删除收货地址
     * @param $validated
     * @return mixed
     */
    public function deleteAddress($validated)
    {
        return (new AddressForms())->deleteAddress(array_merge($validated, ['uid' => UID]));
    }


    /**
     * @param $validated
     * @return mixed
     */
    public function getBehaviorList($validated)
    {
        $validated['uid'] = UID;

        $behavior = (new UserBehaviorForms())->getBehaviorList($validated);

        if(count($behavior)){
            $behavior = $behavior->toArray();

            foreach ($behavior as &$item){
                $item['goods'] = array_merge(
                    (new GoodsForms())->getGoodsDetailById($item['goods_id'])->toArray(),
                    (new GoodsAttributeForms())->setSkuAmountItem($item['goods_id'])
                );

            }
        }

        return $behavior;
    }


    /**
     * @param $validated
     * @return bool|null
     */
    public function deleteBehavior($validated)
    {
        return (new UserBehaviorForms())->deleteBehaviorIds($validated['ids'], ['uid' => UID]);
    }

    /**
     * @return \Illuminate\Support\Collection
     * @throws \App\Exceptions\RequestException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     */
    public function getShareCode()
    {
        (new ShareCodeServices())->generate((new UserForms())->getUserDetail(UID));

        return (new UserForms())->getUserShareCode([
            'uid'  => UID,
            'type' => UserShareCode::USER_SHARE
        ]);
    }
}